﻿using System;
using System.Linq;

class SubsetOfKElementsWithSumS
{
    static int totalSums;

    static void Main()
    {
        //Console.Write("Number of elements of array:");
        //int arrayLength = int.Parse(Console.ReadLine());
        //int[] inputArray = new int[arrayLength];
        //for (int i = 0; i < arrayLength; i++)
        //{
        //    Console.Write("\nElement a[{0}]:", i);
        //    inputArray[i] = (int.Parse(Console.ReadLine()));
        //}
        //Console.Write("How many elements:");
        //int elementsK = int.Parse(Console.ReadLine());
        //Console.Write("Targeted sum:");
        //int expectedSum = int.Parse(Console.ReadLine());

        int[] inputArray = { 2, 1, 2, 4, 3, 5, 2, 6 };
        int expectedSum = 14;
        int elementsK = 4;

        int[] array = new int[inputArray.Length];
        Subset(inputArray.Length - 1, array, inputArray, expectedSum, elementsK);

        if (totalSums == 0)
        {
            Console.WriteLine("No such sum");
        }
        else
        {
            Console.WriteLine("U got {0} sums!", totalSums);
        }
    }

    static void Subset(int n, int[] subsetsArray, int[] inputArray, int expectedSum, int elementsK)
    {
        if (n < 0)
        {
            int subsetSum = 0;
            for (int i = 0; i < subsetsArray.Length; i++)
            {
                if (subsetsArray[i]!=int.MinValue)
                {
                    subsetSum += subsetsArray[i];
                }
            }

            if (subsetSum == expectedSum)
            {
                Print(subsetsArray, elementsK);
            }
            return;
        }

        subsetsArray[n] = int.MinValue;
        Subset(n - 1, subsetsArray, inputArray, expectedSum, elementsK);

        subsetsArray[n] = inputArray[n];
        Subset(n - 1, subsetsArray, inputArray, expectedSum, elementsK);

    }

    static void Print(int[] array, int elementsK)
    {
        int counter = 0;
        for (int i = 0; i < array.Length; i++)
        {
            if (array[i] != int.MinValue)
            {
                counter++;
            }
        }
        if (elementsK != counter)
        {
            return;
        }
        for (int i = 0; i < array.Length; i++)
        {
            if (array[i] != int.MinValue)
            {
                Console.Write(" " + array[i]);
            }
        }
        totalSums++;    
        Console.WriteLine();
    }
}